﻿/*
	Class Description: LineGraph GUI built using graphics API
	------------------------------------------------------------------------
	                          APACHE LICENSE
	------------------------------------------------------------------------
	Copyright 2010 Vu Thien Nguyen Phong
	
	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at
	
	http://www.apache.org/licenses/LICENSE-2.0
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/
package com.minidebugger.gui {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFormat;

	public class LineGraph extends Sprite {
		private const CIRCLE_RADIUS:int = 3;
		
		// width, height and background color of LineGraph
		private var w:int;
		private var h:int;
		private var bgColor:uint;
		
		// list of all lines shown in graph
		private var lines:Array;
		
		public function LineGraph(w:int,h:int,bgColor:uint=0xff000000) {
			this.w = w;
			this.h = h;
			this.bgColor = bgColor;
			lines = new Array();
			
			addEventListener(Event.ADDED_TO_STAGE,init);
		}
		
		private function init(e:Event):void{
			removeEventListener(Event.ADDED_TO_STAGE,init);
			
			// entry point
			this.graphics.beginFill(bgColor);
			this.graphics.drawRect(0,0,w,h);
			this.graphics.endFill();
		}
		
		// adding a line with distinctive color to graph
		public function addLine(color:uint):int{
			var line:Sprite = new Sprite();
			var obj:Object = new Object();
			
			obj.line = line;
			obj.color = color;
			obj.x = 0;
			obj.y = 0;
			lines.push(obj);
			this.addChild(line);
			
			return lines.length-1;
		}
		
		// move line "id" (get from addLine()) - without drawing - to a new position
		public function moveLineTo(id:int,x:int,y:int):void{
			lines[id].line.graphics.moveTo(x,y);
			lines[id].x = x;
			lines[id].y = y;
		}
		
		// move line "id" (get from addLine()) and drawing a long the way to a new position
		public function drawLineTo(id:int,x:int,y:int):void{
			lines[id].line.graphics.lineStyle(1,lines[id].color);
			lines[id].line.graphics.lineTo(x,y);
			lines[id].x = x;
			lines[id].y = y;
		}
		
		public function drawNode(id:int):void{
			lines[id].line.graphics.beginFill(lines[id].color);
			lines[id].line.graphics.drawCircle(lines[id].x,lines[id].y,CIRCLE_RADIUS);
			lines[id].line.graphics.endFill();
		}
		
		// adding the text to line nodes with dx and dy is the different in position of the
		// text with the center of the node
		public function addText(id:int,str:String,dx:Number=0,dy:Number=0):void{
			var tField:TextField = new TextField();
			var tf:TextFormat = new TextFormat();
			tf.color = lines[id].color;
			tf.size = 10;
			tf.bold = true;
			tf.font = "Arial";
			
			tField.defaultTextFormat = tf;
			tField.autoSize = "left";
			tField.text = str;
			tField.x = lines[id].x - tField.textWidth/2 + dx;
			tField.y = lines[id].y - tField.textHeight/2 + dy;
			
			lines[id].line.addChild(tField);
		}
		
		// clear all graphics in a line
		public function clearLine(id:int):void{
			lines[id].line.graphics.clear();
			while (lines[id].line.numChildren>0)
				lines[id].line.removeChildAt(lines[id].line.numChildren-1);
		}
	}
}
